package com.wzy.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzy.base.exception.CustomException;
import com.wzy.content.mapper.CourseBaseMapper;
import com.wzy.content.mapper.CourseTeacherMapper;
import com.wzy.content.model.po.CourseBase;
import com.wzy.content.model.po.CourseTeacher;
import com.wzy.content.service.CourseTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author wzy
 * @Date 2024/1/1 19:06
 * @description: 课程老师相关业务实现
 */
@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {
    @Autowired
    private CourseTeacherMapper courseTeacherMapper;
    @Autowired
    private CourseBaseMapper courseBaseMapper;

    /**
     * 根据课程id查询老师
     * @param courseId 课程id
     * @return
     */
    @Override
    public List<CourseTeacher> getListCourseTeacher(Long courseId) {
        if(courseId==null){
            throw new CustomException("课程id不能为空");
        }
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId,courseId);
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(queryWrapper);
        return courseTeachers;
    }

    /**
     * 新增和修改老师信息 只有机构在属于自己的课程中新增老师信息
     * @param companyId 机构id
     * @param courseTeacher 新增信息
     * @return
     */
    @Override
    public CourseTeacher saveAndUpdateCourseTeacher(Long companyId, CourseTeacher courseTeacher) {
        //根据课程id查询课程信息
        CourseBase courseBase = courseBaseMapper.selectById(courseTeacher.getCourseId());
        if(courseBase==null){
            throw new CustomException("课程信息为空！");
        }
        if(!companyId.equals(courseBase.getCompanyId())){
            throw new CustomException("机构只能修改属于自己的课程");
        }
        if(courseTeacher.getId()==null){
            //当传入id为空的时候 执行新增操作
            courseTeacher.setCreateDate(LocalDateTime.now());
            courseTeacherMapper.insert(courseTeacher);
        }else{
            //id不为空 执行修改操作
            courseTeacherMapper.updateById(courseTeacher);
        }
        CourseTeacher courseTeacherInfo = courseTeacherMapper.selectById(courseTeacher.getId());

        return courseTeacherInfo;
    }

    /**
     * 删除课程老师
     * @param companyId 机构id
     * @param courseId 课程id
     * @param teacherId 老师id
     */
    @Override
    public void deleteCourseTeacher(Long companyId, Long courseId, Long teacherId) {
        //根据课程id查询出对应的课程信息
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        if(courseBase==null){
            throw new CustomException("课程信息为空！");
        }
        if(!companyId.equals(courseBase.getCompanyId())){
            throw new CustomException("机构只能删除属于自己的课程");
        }
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getId,teacherId)
                .eq(CourseTeacher::getCourseId,courseId);
        courseTeacherMapper.delete(queryWrapper);
    }
}
